package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeleteTC
 */
//@WebServlet(description = "刪除測試案例", urlPatterns = { "/DeleteTC" })
public class DeleteTC extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteTC() {
        super();
    }

	/**
	 * 如果有供uid 表示是刪除案例的案例的某個版本，但若沒有提供，就直接刪除整串的案例
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tcID = request.getParameter("q_tcID"); // 測試案例ID
		String uID = "";
		String strSQL = "SELECT uID from tms_tcidmapping where tcID='"+tcID+"'";
		String strRes = ""; // 最後執行的結果回傳
		String strDeleteMapping_1 = "";
		String strDeleteMapping_2 = "";
		
		boolean isDeleteTC = request.getParameter("clickUID") == null?true:false;
		
		if (isDeleteTC) {
			strDeleteMapping_1 = "DELETE FROM tms_tcidmapping WHERE tcID='"+tcID+"'";
		} else {
			uID = request.getParameter("clickUID"); // 測試案例系統內部ID
			strDeleteMapping_1 = "DELETE FROM tms_tcidmapping WHERE tcID='"+tcID+"' AND uID='"+uID+"'";
		}
		
		String creator = request.getParameter("loginUser"); // 創建者
		
		Connection conn = null;
		Widgets wid = new Widgets();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			conn = wid.getConn(true);
			if (isDeleteTC) {
				// 刪除測試案例中的系統記錄
				ps = conn.prepareStatement(strSQL);
				rs = ps.executeQuery();
				while (rs.next()) {
					strDeleteMapping_2 = "DELETE FROM tms_testcase WHERE uID="+rs.getInt("uID");
					ps = conn.prepareStatement(strDeleteMapping_2);
					ps.executeUpdate();
				}
			} else {
				// 刪除測試案例中的系統記錄
				strDeleteMapping_2 = "DELETE FROM tms_testcase WHERE uID="+uID;
				ps = conn.prepareStatement(strDeleteMapping_2);
				ps.executeUpdate();
			}
			
			// 刪除測試案例中的對應記錄
			ps = conn.prepareStatement(strDeleteMapping_1);
			ps.executeUpdate();
			
			if (isDeleteTC) {
				wid.keepLogTC(tcID, Long.valueOf(0), creator, 5, conn, ps);
			} else {
				wid.keepLogTC(tcID, Long.valueOf(uID), creator, 4, conn, ps);	
			}
			
			
			strRes = "刪除測試案例成功!!";

		} catch (SQLException e) {
			strRes = wid.keepErrorMes(conn, ps, e, "", true);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				strRes = e.toString(); 
				System.out.println(strRes);
			}
		}

		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(strRes);

	}

}
